//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include "ddk.h"
#include "CDirStrEnumerator.h"
#define IncrementDllLockCount() do {} while(0)
#define DecrementDllLockCount() do {} while(0)
#include "_CKtfsDirStrEnumerator.cpp"

#include <string.h>
#include <stdlib.h>

ECode CKtfsDirStrEnumerator::Current(
    /* [out] */ WStringBuf * pPsa)
{
    if (m_current[0] == L'\0') {
        return E_INVALID_OPERATION;
    }

    if (pPsa == NULL || pPsa->GetCapacity() < (int)wcslen(m_current)) {
        return E_INVALID_ARGUMENT;
    }

    pPsa->Copy(m_current);

    return NOERROR;
}

ECode CKtfsDirStrEnumerator::MoveNext(
    /* [out] */ Boolean* pSucceeded)
{
	UInt32 uReadSize;

	if (pSucceeded == NULL) {
		return E_INVALID_ARGUMENT;
	}
	
	*pSucceeded = FALSE;
	uReadSize = 0;
    m_current[0] = L'\0';

    Ktfs_Lock(&m_pCFatFile->f_node->n_lock);
    m_pCFatFile->pFatDir->fatfs_read(m_pCFatFile,
                        (unsigned char *)m_current, VSE_MAX_NAME, &uReadSize);
    Ktfs_Unlock(&m_pCFatFile->f_node->n_lock);

    if (m_current[0] == L'\0') {
        return NOERROR;
    }
	
	*pSucceeded = TRUE;
    return NOERROR;
}

ECode CKtfsDirStrEnumerator::Reset()
{
    m_pCFatFile->f_pos = 0;
    m_current[0] = L'\0';

    return NOERROR;
}
